<?php
namespace org;
/**
 *  @file : sms253.php
 *  @brief: https://zz.253.com/v5.html#/api_doc#nav0
 */
class sms253 {

  const USERNAME = "N9098843"; // 验证码通知短信帐号
  const PASSWORD = "Ps2284f1"; // 验证码通知短信密码

  // 发短信
  public static function send($mobile, $content) {
    $data = http_build_query([
      'account' => self::USERNAME,
      'pswd'    => self::PASSWORD,
      'mobile'  => $mobile,
      'msg'     => $content,
      'needstatus'  => 'true',
    ]);
    var_dump(self::getBalance());
    var_dump($data);
    $data = file_get_contents("http://zapi.253.com/msg/HttpBatchSendSM?{$data}");
    $data = trim(str_replace("\n", ',', $data), ',');
    var_dump($data);
    // 
    // $data = explode(',', $data);
    // $len = count($data);
    
    return $data;
  }
  
  // 查余额
  public static function getBalance() {
    $data = http_build_query([
      'account' => self::USERNAME,
      'pswd'    => self::PASSWORD,
    ]);
    $data = file_get_contents("http://zapi.253.com/msg/QueryBalance?{$data}");
    // "20180803191429,103"
    // "20180803191434,0\n220521332,2313"
    $data = str_replace("\n", ',', $data);
    $data = explode(',', $data);
    $len = count($data);
    $eax = 0;
    if ($len == 4) {
      trace("253短信条数预警, 还可以发送[{$data[3]}]条短信");
    } else if ($len == 2) {
      $eax = $data[1];
    }
    return $eax;
  }
  public static function getError($errno) {
    $msg = '';
    switch ($errno){
      case 0:   $msg = "提交成功"; break;
      case 101: $msg = "无此用户"; break;
      case 102: $msg = "密码错"; break;
      case 103: $msg = "提交过快（提交速度超过流速限制）"; break;
      case 104: $msg = "系统忙（因平台侧原因，暂时无法处理提交的短信）"; break;
      case 105: $msg = "敏感短信（短信内容包含敏感词）"; break;
      case 106: $msg = "消息长度错（>536或<=0）"; break;
      case 107: $msg = "包含错误的手机号码"; break;
      case 108: $msg = "手机号码个数错（群发>50000或<=0;单发>200或<=0）"; break;
      case 109: $msg = "无发送额度（该用户可用短信数已使用完）"; break;
      case 110: $msg = "不在发送时间内"; break;
      case 111: $msg = "超出该账户当月发送额度限制"; break;
      case 112: $msg = "无此产品，用户没有订购该产品"; break;
      case 113: $msg = "extno格式错（非数字或者长度不对）"; break;
      case 115: $msg = "自动审核驳回"; break;
      case 116: $msg = "签名不合法，未带签名（用户必须带签名的前提下）"; break;
    }
    return $msg;
  }
}
